﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LinFu.IoC.Configuration;

namespace CoreEx.Database.Query.Linq.Implementation
{
    [Implements(typeof(IAliasMap),LifecycleType.OncePerThread)]
    public class AliasMap : IAliasMap
    {                
        private IDictionary<string,IList<string>> _aliases = new Dictionary<string, IList<string>>();
        private int _aliasCount = -1;

        public void Clear()
        {
            _aliases.Clear();
            _aliasCount = -1;
        }

        public string GetNextAlias(string tableName)
        {
            if (!_aliases.ContainsKey(tableName))
            {
                _aliases.Add(tableName,new List<string>());
            }
            _aliasCount++;
            var nextAlias = string.Format("t{0}", _aliasCount);
            _aliases[tableName].Add(nextAlias);
            

            return nextAlias;
            
        }

        public string GetCurrentAlias(string tableName)
        {
            if (!_aliases.ContainsKey(tableName))
                return GetNextAlias(tableName);

            return _aliases[tableName].Last();
        }




    }
}
